package com.levy.utils.redis;

import com.levy.utils.PropertiesUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.io.IOException;
import java.util.*;

public class RedisConnector {
	private static Logger logger = Logger.getLogger(RedisConnector.class);

	private static JedisCluster cluster;
	private static Set<HostAndPort> clusterNodes;
	RedisConnector() {
		connectRedis();

	}

	private void connectRedis() {
		try {
			clusterNodes = new HashSet<HostAndPort>();
			List<Map<String, Object>> list = getRedisClusterMaps();
			for (Map m : list){
				clusterNodes.add(new HostAndPort(String.valueOf(m.get("ip")),Integer.parseInt(String.valueOf(m.get("port")))));
			}
			cluster = new JedisCluster(clusterNodes);
		}catch (Exception e){
			logger.error("-----RedisConnector[connectRedis]------初始化redis集群操作完成！");
		}


	}


	/**
	 * 从配置文件中获取redis集群地址
	 * @return
	 */
	private List<Map<String, Object>> getRedisClusterMaps() {
		String redisCluster_value = PropertiesUtil.getStringValue("redisCluster_value");

		String[] redisClusterMap = redisCluster_value.split(",");
		List<Map<String,Object>> list  = new ArrayList<Map<String,Object>>();
		for (int i = 0 ; i<redisClusterMap.length;i++){
			Map<String,Object> map = new HashMap<String,Object>();
			String[] dd = redisClusterMap[i].split(":");
			map.put("ip",dd[0]);
			map.put("port",dd[1]);
			list.add(map);
		}
		return list;
	}



	public  void putData(String key,String data) {
			try {
				if (cluster == null) {
					cluster = new JedisCluster(clusterNodes);

				}
					String result = cluster.set(key,data);
				logger.error("-----RedisConnector[putData]------redis集群设置键值对操作完成！键=" + key + "    " + ",值(String)="+ data+",result="+result);
			} catch (Exception e) {
				logger.error("-----RedisConnector[putData]------redis集群设置键值对操作异常！" + e.getMessage());
				e.printStackTrace();
			}

	}
	
	public  void putDataMap(String key,Map<String, String> map ) {
			try {
				if (cluster == null) {
					cluster = new JedisCluster(clusterNodes);

				} 
					String hmset = cluster.hmset(key, map);
				logger.error("-----RedisConnector[putDataMap]------redis集群设置键值对操作完成！键=" + key + "    " + ",值(hashMap)="+ map.toString()+",result="+hmset);
			} catch (Exception e) {
				logger.error("-----RedisConnector[putDataMap]------redis集群设置键值对操作异常！" + e.getMessage());
			}
	}
	
	public  Map<String, String> getDataMap(String key) {
		Map<String, String> data = new HashMap<String, String>();
		try {
			if (cluster == null) {
				cluster = new JedisCluster(clusterNodes);
			} 
			data = cluster.hgetAll(key);
			logger.error("-----RedisConnector[getDataMap]------redis集群获取键值对操作完成！键=" + key + "    " + ",值(hashMap)="+ data.toString());
		} catch (Exception e) {
			logger.error("-----RedisConnector[putDataMap]------redis集群获取键值对操作异常！" + e.getMessage());
		}
		return data;
}

	public  String getData(String key) {
		String data = "";
		try {
			if (cluster == null) {
				cluster = new JedisCluster(clusterNodes);
			}
			data =  cluster.get(key);
			logger.error("-----RedisConnector[getData]------redis集群获取键值对操作完成！键=" + key + "    " + ",值(String)="+ data);
		} catch (Exception e) {
			logger.error("-----RedisConnector[getData]------redis集群获取键值对操作异常！" + e.getMessage());
		}
		return data;
}

	public  void  close() throws IOException {
		try {
			cluster.close();
			logger.error("-----RedisConnector[close]------关闭redis集群操作完成！");
		}catch (Exception e){
			logger.error("-----RedisConnector[close]------关闭redis集群操作异常！" + e.getMessage());
		}

	}
	
	public void del(String key){
		try {
			if (cluster == null) {
				cluster = new JedisCluster(clusterNodes);
			} 
			if (StringUtils.isNotBlank(key)) {
				cluster.del(key);
				logger.error("-----RedisConnector[del]------redis集群删除键值对操作完成！键="+key);
			}else{
				logger.error("-----RedisConnector[del]------redis集群不存在此键！键="+key);
			}
		} catch (Exception e) {
			logger.error("-----RedisConnector[del]------redis集群删除键值对操作异常！"+e.getMessage());
		}
	}
	
	public static void main(String[] args) {

		Map<String,String> map = new HashMap<String,String>();
		map.put("qqq","111");
		map.put("www","222");
		RedisClusterUtil.getInstance().putDataMap("ss",map);
		RedisClusterUtil.getInstance().getDataMap("ss");
//		RedisClusterUtil.getInstance().putData("sad","fdsf");
//		RedisClusterUtil.getInstance().getData("sad");
	}
}
